/**
 * Created By zhangQ 2018/5/7
 * [文件操作帮助类]
 */

import fs from 'fs'

/**
 * TODO(监测路径是否存在)
 * @auther zhangQ
 * @date 2018/5/7 15:32
 */
export function pathExists (path) {
  return fs.existsSync(path)
}

/**
 * TODO(创建多级目录)
 * @auther zhangQ
 * @date 2018/5/7 15:32
 */
export function mkdirs (path) {
  let pathArray = path.replace(/\//g, '\\').split('\\')
  return new Promise((resolve, reject) => {
    fsMkdir(pathArray, 2, () => {
      resolve()
    })
  })
}

/**
 * TODO(创建多级目录)
 * @auther zhangQ
 * @date 2018/5/7 15:32
 */
function fsMkdir (pathArray, index, callBack) {
  let path = pathArray.slice(0, index).join('\\')
  fs.mkdir(path, () => {
    if (index < pathArray.length) {
      index = index + 1
      fsMkdir(pathArray, index, callBack)
    } else {
      callBack()
    }
  })
}

export default {pathExists, mkdirs}
